from util import *
from const import const

CHAPTER_NUM, CHAPTER_NUM2 = 12, 1  # 几-几
CHAPTER_TYPE = 'H' # 普通还是困难
CHAPTER_X, CHAPTER_Y = 210,200  # 几-几的坐标
isBook = False # 是否需要双倍书
first = -1 # 道中队
second = -1 # boss队
third = -1 # 潜艇队
firstWork = -1 # 道中队职责
thirdWork = -1 # 潜艇职责
cnt = sys.maxsize # 需要打几次 默认无限次

def main():
    global cnt # 需要声明
    time.sleep(2)

    if CHAPTER_TYPE == 'N':
        util.logOut(__file__,'普通'+str(CHAPTER_NUM)+'-'+str(CHAPTER_NUM2)+'开始')
    if CHAPTER_TYPE == 'H':
        util.logOut(__file__,'困难'+str(CHAPTER_NUM)+'-'+str(CHAPTER_NUM2)+'开始')
        cnt = 3

    const.chapterProcess(CHAPTER_NUM, CHAPTER_TYPE)
	
    # 判断每日几次 困难特有
    if CHAPTER_TYPE == 'H':
        list = util.getNumbers((60, 407, 93, 429))
        if len(list) > 0:
            cnt = int(list[0][0])

    playCnt = 0 # 游玩次数
    for i in range(cnt):
        playCnt += 1
        if playCnt != 1 and not util.isFindPic(const.publicPath() + 'bmp/exercise.jpg'):
            # 不是第一次的话，简化版本的chapterProcess
            # 优化：得不在选关界面
            const.backMainProcess()
            x, y = const.picLoop(const.publicPath() + 'bmp/weigh anchor.jpg')
            util.click(x, y)
            x, y = const.picLoop(const.publicPath() + 'bmp/main battleline.jpg')
            util.click(x, y)
            const.picLoop(const.publicPath() + 'bmp/exercise.jpg')

        # 进入
        util.logOut(__file__,'-------------第'+str(playCnt)+'局开始------------')
        if playCnt == 1:
            const.intoStageProcess(
                xx = CHAPTER_X, yy = CHAPTER_Y, 
                first = first, second = second, 
                isWeek = True, 
                isBook = isBook,
                third = third, 
                firstWork = firstWork, 
                thirdWork = thirdWork
            )
        else:
            # 第二次不做判断，直接进去了
            util.click(CHAPTER_X, CHAPTER_Y)
            x, y = const.picLoop(const.publicPath() + 'bmp/start chapter.jpg')
            util.click(x,y)
            time.sleep(2)
            x, y = const.picLoop(const.publicPath() + 'bmp/start chapter.jpg')
            time.sleep(1)
            util.click(x,y)

        util.logOut(__file__,'红脸判断 开始')
        time.sleep(1.000)
        list = util.getWords((237, 201 , 627, 263))
        if len(list) != 0 and '低心情' in list[0]:
            util.logOut(__file__,'红脸')
            util.logOut(__file__,'-------------第'+str(playCnt)+'局结束-------------')
            util.logOut(__file__,'等待30分钟，回心情...')
            time.sleep(60*30)
            const.backMainProcess()
            i = i - 1
            continue

        x, y, url = const.picMulLoop([
            const.publicPath() + 'bmp/OK.jpg', # 判断红脸
            const.publicPath() + 'bmp/death.jpg', # 打不过死了
            const.publicPath() + 'bmp/again.jpg', # 正常退出
        ], 60*15)
        if url == const.publicPath() + 'bmp/OK.jpg': # 判断红脸
            isRedFace = False
            util.logOut(__file__,'红脸判断 开始')
            list = util.getWords((237, 201 , 627, 263))
            if len(list) != 0:
                for m in list:
                    if '低心情' in m:
                        isRedFace = True
                        break
            if isRedFace:
                util.logOut(__file__,'红脸 退出战斗')
                x, y = const.picLoop(const.publicPath() + 'bmp/quit.jpg')
                util.click(x, y)
                x, y = const.picLoop(const.publicPath() + 'bmp/withdraw.jpg')
                util.click(x, y)
                x, y = const.picLoop(const.publicPath() + 'bmp/OK.jpg')
                util.click(x, y)
                x, y = const.picLoop(const.publicPath() + 'bmp/leave.jpg')
                util.click(x, y)
                util.logOut(__file__,'-------------第'+str(playCnt)+'局结束-------------')
                util.logOut(__file__,'等待30分钟，回心情...')
                time.sleep(60*30)
                const.backMainProcess()
                i = i - 1
                continue
            else:
                x, y = util.findPic(const.publicPath() + 'bmp/oil_less.jpg')
                if x!=-1 and y!=-1:
                    # 没有油了
                    util.logOut(__file__,'-------------第'+str(playCnt)+'局结束-------------')
                    util.logOut(__file__,'没有石油了，脚本结束')
                    const.backMainProcess()
                    return
        elif url == const.publicPath() + 'bmp/again.jpg': # 正常退出
            pass
        elif url == const.publicPath() + 'bmp/death.jpg': # 打不过死了
            util.logOut(__file__,'打不过死了')
            util.click(x, y)
            x, y = const.picLoop(const.publicPath() + 'bmp/quit.jpg')
            util.click(x, y)
            x, y = const.picLoop(const.publicPath() + 'bmp/withdraw.jpg')
            util.click(x, y)
            x, y = const.picLoop(const.publicPath() + 'bmp/OK.jpg')
            util.click(x, y)
            x, y = const.picLoop(const.publicPath() + 'bmp/leave.jpg')
            util.click(x, y)
            const.backMainProcess()
            util.logOut(__file__,'-------------第'+str(playCnt)+'局结束-------------')
            i = i - 1
            continue

        util.logOut(__file__,'-------------第'+str(playCnt)+'局结束-------------')
        x, y = const.picLoop(const.publicPath() + 'bmp/leave.jpg')
        util.click(x, y)

        # 退役操作 普通特有
        if CHAPTER_TYPE == 'N':
            time.sleep(2)
            const.backMainProcess()
            x, y = const.picLoop(const.publicPath() + 'bmp/build.jpg')
            util.click(x, y)
            x, y = const.picLoop(const.publicPath() + 'bmp/build_retire.jpg')
            util.click(x, y)
            if playCnt == 1:
                const.retireProcess(third = 3)
            else:
                const.retireProcess(third = 3, skipOption = True)

    const.backMainProcess()